// ==================================================
// Copyright 2014 (C) , dotLogix
// Author: Alexander Schill <alexander@schillnet.de>.
// ==================================================

namespace DotLogix {
    #region Using Directives
    using System;
    using System.Windows;
    using System.Windows.Media;

    #endregion

    public class AutoSizeTextWithBackground : AutoSizeText {
        private TranslateTransform _transform;
        private Brush _background;
        private HorizontalAlignment _horizontalContentAlignment = HorizontalAlignment.Stretch;
        private VerticalAlignment _verticalContentAlignment = VerticalAlignment.Stretch;

        public VerticalAlignment VerticalContentAlignment {
            get { return _verticalContentAlignment; }
            set {
                _verticalContentAlignment = value;
                InvalidateMeasure();
            }
        }

        public HorizontalAlignment HorizontalContentAlignment {
            get { return _horizontalContentAlignment; }
            set {
                _horizontalContentAlignment = value;
                InvalidateMeasure();
            }
        }

        public virtual Brush Background {
            get { return _background; }
            set {
                _background = value;
                InvalidateVisual();
            }
        }

        protected override void OnRender(DrawingContext drawingContext) {
            drawingContext.DrawRectangle(_background, null, new Rect(DesiredSize));
            drawingContext.PushTransform(_transform);
            base.OnRender(drawingContext);
            drawingContext.Pop();
        }

        protected override Size MeasureOverride(Size availableSize) {
            double x, y;
            var measured = base.MeasureOverride(availableSize);
            switch(VerticalContentAlignment) {
                case VerticalAlignment.Top:
                    y = 0;
                    break;
                case VerticalAlignment.Bottom:
                    y = availableSize.Height - measured.Height;
                    break;
                case VerticalAlignment.Stretch:
                case VerticalAlignment.Center:
                    y = (availableSize.Height - measured.Height) / 2;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            switch(HorizontalAlignment) {
                case HorizontalAlignment.Left:
                    x = 0;
                    break;

                case HorizontalAlignment.Right:
                    x = availableSize.Width - measured.Width;
                    break;
                case HorizontalAlignment.Stretch:
                case HorizontalAlignment.Center:
                    x = (availableSize.Width - measured.Width) / 2;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            _transform = new TranslateTransform(x, y);
            return availableSize;
        }
    }
}